import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.kit.PaymentKit;

import java.util.HashMap;
import java.util.Map;

/**
 * @authc yaowk
 * 2017/6/16
 */
public class PayService {

    private static final String MCH_ID = "12006";
    private static final String KEY = "bdb783b8f07b497fa11d4bd0fd2f60cb";
    private Log log = Log.getLog("");

    public Map sendPay(String money, Integer payTyep) {
        Map<String, String> map = new HashMap<>();
        if (payTyep == 1) {
            map.put("service", "YSWXSM");
        }
        else if (payTyep == 2) {
            map.put("service", "YSZFBSM");
        }
        map.put("version", "1.0");
        map.put("charset", "UTF-8");
        map.put("sign_type", "MD5");
        map.put("mch_id", MCH_ID);
        map.put("out_trade_no", StrKit.getRandomUUID());
        map.put("body", "body");
        map.put("total_fee", money);
        map.put("mch_create_ip", "127.0.0.1");
        map.put("notify_url", "http://103.56.19.93/notifyUrl");
        map.put("callback_url", "http://103.56.19.93/callbackUrl");
        map.put("nonce_str", StrKit.getRandomUUID());
        map.put("attach", "att");
        String sign = PaymentKit.createSign(map, KEY);
        map.put("sign", sign);
        String body = PaymentKit.toXml(map);
        String receive = HttpKit.post("http://139.199.248.24/pay/gateway", body);
        Map<String, String> receiveMap = PaymentKit.xmlToMap(receive);
        return receiveMap;
    }

    public Map orderQuery() {
        Map<String, String> map = new HashMap<>();
        map.put("version", "1.0");
        map.put("charset", "UTF-8");
        map.put("sign_type", "MD5");
        map.put("mch_id", MCH_ID);
        map.put("out_trade_no", "124");
//        map.put("transaction_id", "");
        map.put("nonce_str", StrKit.getRandomUUID());
        String sign = PaymentKit.createSign(map, KEY);
        map.put("sign", sign);
        String body = PaymentKit.toXml(map);
        System.out.println(body);
        String receive = HttpKit.post("http://139.199.248.24/pay/orderquery", body);
        System.out.println(receive);
        Map<String, String> receiveMap = PaymentKit.xmlToMap(receive);
        return receiveMap;
    }
}
